package nl.boektrust.btsoep.shared.model;

/**
 * 
 */

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
import java.io.Serializable;

import javax.persistence.Id;
import javax.persistence.Entity;

import com.googlecode.objectify.annotation.Indexed;
import com.googlecode.objectify.annotation.Subclass;
import com.googlecode.objectify.annotation.Unindexed;



/**
 * @author rao3
 *
 */
@Subclass
public class Person extends BTEntity implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5095282642166804467L;
	@Unindexed private String biographical;
	@Unindexed private String birthPlace;
	@Unindexed private int birthYear;
	@Unindexed private String deceasedPlace;
	@Unindexed private int deceasedYear;
	@Unindexed private boolean isDeceased;
	private String identifier_dai;
	@javax.persistence.Transient private boolean isSelected;
	@javax.persistence.Transient private List<PhysicalBookManifestation> booksContributedTo;
	@Indexed private ArrayList<String> parsedName;

	/**
	 * @return the identifier_dai
	 */
	public String getIdentifier_dai() {
		return identifier_dai;
	}

	/**
	 * @param identifier_dai the identifier_dai to set
	 */
	public void setIdentifier_dai(String identifier_dai) {
		this.identifier_dai = identifier_dai;
	}

	
	/**
	 * @param name
	 * @param url
	 * @param id
	 */
	public Person(String name, String url) {
		super(name, url);
		// TODO Auto-generated constructor stub
		this.setDeceasedYear(-1);
		this.setBirthYear(-1);
		this.isDeceased=false;
		this.isSelected=false;
	}
	
	public Person() {
		this("", "");
	}

	/**
	 * @return the biographical
	 */
	public String getBiographical() {
		return biographical;
	}
	
	public boolean isSelected() {
		return this.isSelected;
	}
	
	public void setSelected(boolean selected) {
		this.isSelected = selected;
	}
	

	/**
	 * @param biographical the biographical to set
	 */
	public void setBiographical(String biographical) {
		this.biographical = biographical;
	}

	/**
	 * @return the birthPlace
	 */
	public String getBirthPlace() {
		return birthPlace;
	}

	/**
	 * @param birthPlace the birthPlace to set
	 */
	public void setBirthPlace(String birthPlace) {
		this.birthPlace = birthPlace;
	}

	/**
	 * @return the birthYear
	 */
	public int getBirthYear() {
		return birthYear;
	}

	/**
	 * @param birthYear the birthYear to set
	 */
	public void setBirthYear(int birthYear) {
		this.birthYear = birthYear;
	}
	
	public void setBirthYear(Integer birthYear) {
		this.birthYear = -1;
	}

	/**
	 * @return the deceasedPlace
	 */
	public String getDeceasedPlace() {
		return deceasedPlace;
	}

	/**
	 * @param deceasedPlace the deceasedPlace to set
	 */
	public void setDeceasedPlace(String deceasedPlace) {
		this.deceasedPlace = deceasedPlace;
	}

	/**
	 * @return the deceasedYear
	 */
	public int getDeceasedYear() {
		return deceasedYear;
	}

	/**
	 * @param deceasedYear the deceasedYear to set
	 */
	public void setDeceasedYear(int deceasedYear) {
		this.deceasedYear = deceasedYear;
	}
	
	/**
	 * @return true if  deceasedYear != -1 
	 */
	public boolean isDeceased() {
		
		if (this.deceasedYear == -1) return false;
		return true;
	}
	/**
	 * @return a compressed string with the birth year/place and deceased year/place 
	 */
	public String displayableBirthDeceasedString() {
		String returnString = new String("");
		if ((this.birthPlace != null && this.birthPlace.isEmpty() == false) || this.birthYear > -1) {
			returnString = returnString + "* ";
			if (this.birthPlace != null && this.birthPlace.isEmpty() == false) {
				returnString = returnString + this.birthPlace;
				if (this.birthYear > -1) {
					returnString = returnString + ", ";
				}
			}
			if (this.birthYear > -1) {
				returnString = returnString + new Integer(this.birthYear).toString();
			} 
		}
		if ((this.deceasedPlace != null && this.deceasedPlace.isEmpty() == false) || this.deceasedYear > -1) {
			returnString = returnString + " + ";
		
			if (this.deceasedPlace != null && this.deceasedPlace.isEmpty() == false) {
				returnString = returnString + this.deceasedPlace;
				if (this.deceasedYear > -1) {
					returnString = returnString + ", ";
				}
			}
			if (this.deceasedYear > -1) {
				returnString = returnString + new Integer(this.deceasedYear).toString();
			} 
		}
		return returnString;
	}
	
	//@javax.persistence.PrePersist
	//@javax.persistence.PreUpdate
	public void populateParsedNameArray() {
		String str = this.getName();
		 this.parsedName = new ArrayList<String>();
		 String[] result = str.split("\\s");
	     for (int x=0; x<result.length; x++) {
	    	 if (StopWords.isANameStopWord(result[x].toLowerCase()) == false) this.parsedName.add(result[x].toLowerCase());
	     }
	}
	
	public void setName(String name) {
		super.setName(name);
		this.populateParsedNameArray();
	}

	/**
	 * @return the numberOfBooksContributedTo
	 */
	public int getNumberOfBooksContributedTo() {
		int numberOfBooksContributedTo = 0;
		if (this.getBooksContributedTo() != null) {
			numberOfBooksContributedTo = this.getBooksContributedTo().size();
		}
		return numberOfBooksContributedTo;
		
	}
	


	/**
	 * @return the booksContributedTo
	 */
	public List<PhysicalBookManifestation> getBooksContributedTo() {
		return booksContributedTo;
	}

	/**
	 * @param booksContributedTo the booksContributedTo to set
	 */
	public void setBooksContributedTo(List<PhysicalBookManifestation> booksContributedTo)	 {
		this.booksContributedTo = booksContributedTo;
	}
	
	public boolean equals(Object p) {
		if (p == null) return false;
		if (!p.getClass().equals(Person.class)) return false;
		return this.id == ((Person)p).id;
	}
	
	public int hashCode() {
		return this.id.intValue();
	}
	
}